package com.back.demo.controller;


import com.back.demo.common.CommonResult;
import com.back.demo.controller.myVO.basicVO.RegisterReqVO;
import com.back.demo.controller.myVO.basicVO.RegisterResVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

@Api(tags = "演示接口")
@RestController
@RequestMapping("/my-api/demo")
public class DemoServer {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation("Hello World接口")
    @GetMapping("/hello")
    public String hello() {
        return "Hello, world!";
    }

    @ApiOperation("测试接口")
    @PostMapping("/test")
    public CommonResult<RegisterResVO> hello(@RequestBody @Valid RegisterReqVO reqVO) {
        RegisterResVO result = new RegisterResVO();
        result.setRegisterResult("测试结果");
        return CommonResult.success(result);
    }

    @GetMapping("/redis")
    public CommonResult<String> redis(@RequestParam(value = "param") String param) {
        //存储 key为aaa，value为17886789645，存在时间为5分钟
        redisTemplate.opsForValue().set("aaa", "17886789645", 5, TimeUnit.MINUTES);
        String string = redisTemplate.opsForValue().get(param);
        return CommonResult.success(string);
    }
}